<?php
/**
 * 供应商管理
 */
namespace Admin\Controller;
use Admin\BaseController;
use Think\Page;
import('ThirdLib.Snoopy',WEB_ROOT);
import('SoeLib.UHCore',WEB_ROOT);
/*Ajax异步获取数据Action*/
class VendorController extends BaseController{

	/**
	 * 商品列表页
	 * Author: shijy
	 */
	public function index(){
		$vendorM = M("cfg_xy_vendor",null,DB_MAIN_CFG);
		import("ORG.Util.Page");
		$count = $vendorM->count();
		$Page = new Page($count,10);
		$show = $Page->show(); // 分页显示输出
		$list = $vendorM->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->smarty->assign('page',$show);
		$this->smarty->assign('list',$list);
		$this->smarty->assign('action','index');
		$this->smarty->display('Vendor/index.html');
	}

	//商品编辑页
	public function edit(){
		$id=I('get.id',0,'int');
		if($id<=0){
			$this->error('请选定供应端进行编辑');
		}

		$vendorM=M('cfg_xy_vendor',null,DB_MAIN_CFG);
		$baseInfo=$vendorM->find($id);
		if(!$baseInfo){
			$this->error('选定供应端并不存在');
		}
		$this->smarty->assign('obj',$baseInfo);
		$this->smarty->display('Vendor/edit.html');
	}


	//商品编辑页
	public function add(){
		$this->smarty->display('Vendor/edit.html');
	}

	public function update(){

		$vendorId=I('post.vid',0,'int');

		$vName=I('post.vName','');
		if(strlen($vName)<2){
			$this->error('请设定供应端名称');
		}

		$vUrl=I('post.vUrl','','/[a-zA-z]+:\/\/[^\s]*/');
		if(strlen($vUrl)<2){
			$this->error('请设定供应端网址');
		}

		$vPhone=I('post.vPhone');
		$isEnable=I('post.isEnable',1,'int');
		$data=array(
			'vid'=>$vendorId,
			'vName'=>$vName,
			'vUrl'=>$vUrl,
			'vPhone'=>$vPhone,
			'isEnable'=>$isEnable
		);

		$vendorM = M("cfg_xy_vendor",null,DB_MAIN_CFG);
		if($vendorM->where("vid={$vendorId}")->find()){
			$vendorM->where("vid={$vendorId}")->save($data);
			$this->success("修改成功");
		}else{
			$vendorM->add($data);
			$this->success("添加成功");
		}
	}

	//类目列表页
	public function close(){
		$vendorId=I('get.id',0,'int');
		if($vendorId<=0){
			$this->error('请选择指定供应端');
		}

		$vendorM = M("cfg_xy_vendor",null,DB_MAIN_CFG);
		$disable=0;
		$vendorM->where("vid={$vendorId}")->setField('isEnable',$disable);
		$this->success("操作成功");
	}

}
